/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.bean;

import desu.nya.DAO.AnimeFactory;
import desu.nya.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Fess
 */
@ManagedBean
@RequestScoped
public class FilterBean {
    private List<SelectItem> studioSI;
    private List<SelectItem> typeSI;
    private List<SelectItem> genreSI;
    private List<SelectItem> yearsSI;
    private List<Anime> animeList;
    private String filterStudio;
    private String filterTypeEd;
    private String filterGenre;
    private String filterGenreExtra;
    private String filterYear;
    private String filterName;
    private String filterCount;

    public String getFilterCount() {
        return filterCount;
    }

    public void setFilterCount(String filterCount) {
        this.filterCount = filterCount;
    }
    
    

    public List<SelectItem> getGenreSI() {
        return genreSI;
    }

    public List<SelectItem> getStudioSI() {
        return studioSI;
    }

    public List<SelectItem> getTypeSI() {
        return typeSI;
    }

    public List<SelectItem> getYearsSI() {
        return yearsSI;
    }

    public List<Anime> getAnimeList() {
        return animeList;
    }

    public String getFilterGenre() {
        return filterGenre;
    }

    public String getFilterGenreExtra() {
        return filterGenreExtra;
    }

    public String getFilterName() {
        return filterName;
    }

    public String getFilterStudio() {
        return filterStudio;
    }

    public String getFilterTypeEd() {
        return filterTypeEd;
    }

    public String getFilterYear() {
        return filterYear;
    }

    public void setFilterGenre(String filterGenre) {
        this.filterGenre = filterGenre;
    }

    public void setFilterGenreExtra(String filterGenreExtra) {
        this.filterGenreExtra = filterGenreExtra;
    }

    public void setFilterName(String filterName) {
        this.filterName = filterName;
    }

    public void setFilterStudio(String filterStudio) {
        this.filterStudio = filterStudio;
    }

    public void setFilterTypeEd(String filterTypeEd) {
        this.filterTypeEd = filterTypeEd;
    }

    public void setFilterYear(String filterYear) {
        this.filterYear = filterYear;
    }
    
    @PostConstruct
    private void replenish(){
        int startYear=2000;
        Calendar cal=Calendar.getInstance();
        cal.setTime(new Date());
        int currYear=cal.get(Calendar.YEAR);
        
        studioSI=new ArrayList<SelectItem>();
        typeSI=new ArrayList<SelectItem>();
        genreSI=new ArrayList<SelectItem>();
        yearsSI=new ArrayList<SelectItem>();
        List<Genre> genreList=null;
        List<String> nickList=null;
        animeList=null;
        List<Studio> studioList=null;
        List<TypeEd> typeList=null;
        List<StreamedContent> coverStreamList=null;
        List<Cover> coverList=null;
        AnimeFactory factory;
        try {
            factory=AnimeFactory.getInstance();
            studioList=factory.getStudioDAO().getAllStudio();
            typeList=factory.getTypeEdDAO().getAllTypeEd();
            genreList=factory.getGenreDAO().getAllGenre();
            nickList=factory.getOtakuDAO().getAllOtaku();
            animeList=factory.getAnimeDAO().getAllAnime();
            coverList=factory.getCoverDAO().getAllCover();
        } catch (SQLException ex) {
            Logger.getLogger(FilterBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        studioSI.add(new SelectItem(-1,"Not set"));
        typeSI.add(new SelectItem(-1,"Not set"));
        yearsSI.add(new SelectItem(-1,"Not set"));
        genreSI.add(new SelectItem(-1,"Not set"));
        
        for(Studio studio:studioList){
            studioSI.add(new SelectItem(studio.getId(),studio.getStudioName()));
        }
        for(TypeEd type:typeList){
            typeSI.add(new SelectItem(type.getId(),type.getTypeName()));
        }
        for(Genre genre:genreList){
            genreSI.add(new SelectItem(genre.getId(),genre.getGenreName()));
        }
        for(int i=startYear;i<=currYear;i++){
            String tStr=String.valueOf(i);
            yearsSI.add(new SelectItem(tStr,tStr));
        }
    }
    
    public void filterClick(){
        try {
            AnimeFactory factory;
            factory=AnimeFactory.getInstance();
            Long fs=Long.valueOf(filterStudio);
            if(fs==-1)fs=null;
            Long fte=Long.valueOf(filterTypeEd);
            if(fte==-1)fte=null;
            Long fg=Long.valueOf(filterGenre);
            if(fg==-1)fg=null;
            Long fge=Long.valueOf(filterGenreExtra);
            if(fge==-1)fge=null;
            int year=Integer.parseInt(filterYear);
            int count=Integer.parseInt(filterCount);
            animeList=(List<Anime>)factory.getAnimeDAO().getAnimeWithFilter(filterName, fs, fte, fg, fge,year,count);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
